import DestructibleFlyer from "../flyer/DestructibleFlyer";
import Flyer from "../flyer/Flyer";
import HealthPoint, { Killer } from "../funcObj/interface/HealthPoint";
import { blockSize } from "../info/Predefine";
import Player from "../player/Player";
import DestructibleFacility from "./DestructibleFacility";

const { ccclass, property } = cc._decorator;

/**
 * 防御炮
 * 使用系统的碰撞检测
 * 遇到敌方飞行物或设施时加入目标列表,离开时退出列表,先入先出
 */
@ccclass
export default abstract class DefensiveFacility extends DestructibleFacility {

    /**
     * 入侵者飞行物
     */
    intrudersFlyer: DestructibleFlyer[];

    /**
     * 范围内的敌方设施 
     */
    intrudersFacility: DestructibleFacility[];

    /**
     * 用于检测敌人是否在范围内
     */
    detectionCollider: cc.CircleCollider;

    public get shotRange(): number {
        return this.facilityInfo.射程半径;
    }

    init(player: Player): void {
        super.init(player);
        this.intrudersFlyer = [];
        this.intrudersFacility = [];
        this.detectionCollider = this.node.getComponentsInChildren(cc.CircleCollider).find(coll => coll.tag == 1);
        this.detectionCollider.radius = this.shotRange * blockSize;
    }

    onCollisionEnter(other: cc.Collider, self: cc.Collider) {
        //0 设施或飞行物本体
        //1 是攻击范围检测
        if (other.tag == 1 || self.tag == 0) return;

        let facility: DestructibleFacility = other.node.parent.getComponent(DestructibleFacility)
        if (facility) {
            if (facility.player == this.player) return;
            this.intrudersFacility.push(facility);
        } else {
            let flyer = other.node.parent.getComponent(DestructibleFlyer);
            if (flyer) {
                if (flyer.player == this.player) return;
                this.intrudersFlyer.push(flyer);
            }
        }
    }

    onCollisionExit(other: cc.Collider, self: cc.Collider) {
        if (other.tag == 1 || self.tag == 0) return;

        let flyer = other.node.parent.getComponent(Flyer);
        if (flyer) {
            let index = this.intrudersFlyer.findIndex(item => item == flyer);
            if (index >= 0) {
                this.intrudersFlyer.splice(index, 1);
            }
        } else {
            let facility = other.node.parent.getComponent(DestructibleFacility);
            if (facility) {
                let index = this.intrudersFacility.findIndex(item => item == facility);
                if (index >= 0) {
                    this.intrudersFacility.splice(index, 1);
                }
            }
        }
    }

    /**
     * 获得入侵者，攻击最先进来的。优先攻击飞行物，设施其次
     * @returns 
     */
    getClosestTarget(): HealthPoint {
        let target: HealthPoint = null;
        if (this.intrudersFlyer.length > 0) {
            target = this.intrudersFlyer[0];
        } else if (this.intrudersFacility.length > 0) {
            target = this.intrudersFacility[0];
        }
        return target;
    }

    die(killer: Killer): void {
        super.die(killer);
        this.intrudersFlyer = [];
        this.intrudersFacility = [];
    }
}
